﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;

namespace hozorghiab
{
    public partial class naghesia : Form
    {
        public sql sqlquery;
        public naghesia(sql s)
        {
            InitializeComponent();
            sqlquery = s;
        }

        private void naghesia_Load(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            PersianCalendar per = new PersianCalendar();
            string year = per.GetYear(DateTime.Now).ToString();
            string month = per.GetMonth(DateTime.Now).ToString();
            yeartoolStripTextBox1.Text = year;
            monthtoolStripComboBox1.Text = month;

        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

            //Dictionary<int, int> naghes = new Dictionary<int, int>();


        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            Dictionary<int, int> naghes = new Dictionary<int, int>();
            if (yeartoolStripTextBox1.Text != "" && yeartoolStripTextBox1.Text.Length == 4)
            {
                /*sqlquery.open();
                SqlDataReader data = sqlquery.ExecuteQuery("SELECT employeid,count(employeid) FROM entry WHERE month=" + monthtoolStripComboBox1.Text.Trim() + " and year=" + yeartoolStripTextBox1.Text.Trim() + " GROUP BY employeid");
                if (data.HasRows)
                {
                    string where = "";
                    while (data.Read())
                    {
                        if ((int)data[1] % 2 != 0)
                        {
                            where += "employeid=" + (int)data[0] + " or ";
                        }
                    }
                    sqlquery.close();
                    if (where.Length > 4)
                    {
                        sqlquery.open();
                        SqlDataAdapter data2 = sqlquery.sqlcommand("SELECT rtrim(name)+' '+rtrim(family) as [نام و نام خانوادگی],unit as [واحد] FROM profile WHERE " + where.Substring(0, where.Length - 4));
                        DataTable dt = new DataTable();
                        data2.Fill(dt);
                        dataGridView1.DataSource = dt;
                        sqlquery.close();
                    }
                }*/
                sqlquery.open();
                SqlDataAdapter data = sqlquery.sqlcommand(@"
                DECLARE @day int
                DECLARE @month int
                DECLARE @year int
                set @day=1
                set @month=" + monthtoolStripComboBox1.Text + @"
                set @year=" +yeartoolStripTextBox1.Text+@"
                DECLARE @Table table(
                    employeid2 int NOT NULL,
                    count int,
                    day tinyint,
                    month tinyint,
	                year int
                );
                while @day<=31
                BEGIN
	                INSERT INTO @Table(day,month,year,employeid2,count) SELECT @day,@month,@year,employeid,count(employeid) FROM entry WHERE month=@month and year=@year and day=@day GROUP BY employeid
	                set @day=@day+1
                END
                SELECT employeid as[شماره کارمندی],rtrim(name)+' '+rtrim(family) as [نام و نام خانوادگی] ,ltrim(str(year))+'/'+ltrim(str(month))+'/'+ltrim(str(day)) as [تاریخ],unit as [واحد] FROM @Table,profile WHERE profile.employeid=employeid2 and count%2=1
            ");
                DataTable dt = new DataTable();
                data.Fill(dt);
                dataGridView1.DataSource = dt;
                sqlquery.close();
            }

        }


    }
}
